/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: BackendApiController
 * Author:   Allen
 * Date:     2020/8/23 14:22
 * Description: 后端开放API业务处理Controller
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bim.marvel.openapi.client.dto.BackendApiDTO;
import com.bim.marvel.openapi.client.query.BackendApiDeleteQuery;
import com.bim.marvel.openapi.client.query.BackendApiQuery;
import com.bim.marvel.openapi.client.query.BackendApiUpdateQuery;
import com.bim.marvel.openapi.client.query.PageQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimplePageVO;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.common.util.SimpleConverter;
import com.bim.marvel.openapi.core.entity.BackendApiDO;
import com.bim.marvel.openapi.core.service.impl.BackendApiService;
import com.bim.marvel.rest.BackendApiController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈后端开放API业务处理Controller〉
 *
 * @author Allen
 * @date 2020/8/23
 * @since 1.0.0
 */
@RestController
public class BackendApiControllerImpl implements BackendApiController {

    /**
     * 注入BackendApiService
     */
    @Autowired
    private BackendApiService backendApiService;

    /**
     * 保存后端开放API接口信息
     * @param backendApiQuery 后端接口实体
     * @return 保存状态
     */
    @Override
    public SimpleVO<Boolean> saveBackendApi(BackendApiQuery backendApiQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = backendApiService.saveBackendApi(backendApiQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.INSERT_FAILED);
        }
        return simpleVO;
    }

    @Override
    public SimpleVO<Boolean> updateBackendApi(BackendApiUpdateQuery backendApiQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = backendApiService.updateBackendApiById(backendApiQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.UPDATE_FAILED);
        }
        return simpleVO;
    }

    @Override
    @CacheEvict(value = "getOpenApiUri", key = "'openApiUrl:' + #backendApiDeleteQuery.serviceId +'_'"
            + "+ #backendApiDeleteQuery.path")
    public SimpleVO<Boolean> removeById(BackendApiDeleteQuery backendApiDeleteQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = backendApiService.removeById(backendApiDeleteQuery.getId());
        if (!success) {
            return new SimpleVO(CodeEnum.DELETE_FAILED);
        }
        return simpleVO;
    }

    @Override
    public SimplePageVO<List<BackendApiDTO>> listBackendApi(BackendApiQuery backendApiQuery, PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageIndex(), pageQuery.getPageSize());
        BackendApiDO backendApiDO = new BackendApiDO();
        BeanUtils.copyProperties(backendApiQuery, backendApiDO);
        QueryWrapper<BackendApiDO> queryWrapper = new QueryWrapper<>(backendApiDO);
        IPage<BackendApiDO> backendApiDOIPage = backendApiService.page(page, queryWrapper);
        List<BackendApiDO> backendApiDOList = backendApiDOIPage.getRecords();
        if (CollectionUtils.isEmpty(backendApiDOList)) {
            return new SimplePageVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<BackendApiDTO> backendApiDTOList = SimpleConverter.convert(backendApiDOList, BackendApiDTO.class);
        return new SimplePageVO(backendApiDTOList, backendApiDOIPage);
    }

    @Override
    public SimpleVO<Boolean> refreshBackendApi() {
        List<BackendApiDO> backendApiDOList = backendApiService.list();
        backendApiDOList.forEach(backendApiDO -> {
            backendApiService.getByServiceIdAndPath(backendApiDO.getServiceId(), backendApiDO.getPath());
        });
        return new SimpleVO(CodeEnum.SUCCESS);
    }
}
